package demo2;

/**
 * @Author GocChin
 * @Date 2021/5/11 13:37
 * @Blog: itdfq.com
 * @QQ: 909256107
 * @Descript: 创建三个窗口买票，总票数为100张，使用Runable接口的方式
 * 存在线程安全问题，待解决
 */
class Thread3 implements Runnable {

    private int ticket = 100;


    @Override
    public void run() {
        while (true) {
            show();
        }

    }
    private synchronized void show(){
        if (ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":买票，票号为：" + ticket);
            ticket--;
        }
    }
}

public class Test3 {
    public static void main(String[] args) {
        Thread3 thread3 = new Thread3();
        Thread t1 = new Thread(thread3);
        Thread t2 = new Thread(thread3);
        Thread t3 = new Thread(thread3);
        t1.setName("窗口一");
        t2.setName("窗口二");
        t3.setName("窗口三");
        t1.start();
        t2.start();
        t3.start();
    }
}
